package com.poh.controller;

import com.poh.pojo.Course;
import com.poh.pojo.Reply;
import com.poh.pojo.Topic;
import com.poh.pojo.User;
import com.poh.service.ReplyService;
import com.poh.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TopicController {
    @Autowired
    TopicService topicService;
    @Autowired
    ReplyService replyService;

    @GetMapping("/toTopic")
    public String toTopic() {
        return "topic/topic";
    }

    @GetMapping("/getTeaTopic/{courseId}")
    public String getTeaTopic(@PathVariable("courseId") int courseId, Model model) {
        List<Topic> topics = topicService.getTopicByCourseId(courseId);
        System.out.println(topics);
        model.addAttribute("topics", topics);
        model.addAttribute("courseId", courseId);
        return "topic/teaTopic";
    }
    @GetMapping("/getTopic/{courseId}")
    public String getTopic(@PathVariable("courseId") int courseId, Model model) {
        List<Topic> topics = topicService.getTopicByCourseId(courseId);
        System.out.println(topics);
        model.addAttribute("topics", topics);
        model.addAttribute("courseId", courseId);
        return "topic/topic";
    }
    @PostMapping("/addTopic")
    public String addTopic(Topic topic, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Course course = (Course) session.getAttribute("course");
        topic.setCourseId(course.getCourseId());
        topic.setUserId(user.getUserId());
        topicService.addTopic(topic);
        return "redirect:/getTopic/" + course.getCourseId();
    }
    @GetMapping("/intoTopic/{topicId}")
    public String intoTopic(@PathVariable("topicId") int topicId, Model model) {
        Topic topic = topicService.getTopicById(topicId);
        List<Reply> replies = replyService.queryReplyByTopicId(topicId);
        if (replies != null) {
            model.addAttribute("replies", replies);
        }
        List<Reply> replyed = replyService.queryAllReplyByTopicId(topicId);
        if (replyed != null) {
            model.addAttribute("replyed", replyed);
        }
        System.out.println(replyed);
        model.addAttribute("topic", topic);
        return "topic/topicInfo";
    }
    @GetMapping("/toMyTopic/{userId}")
    public String toMyTopic(@PathVariable("userId") int userId, Model model) {
        List<Topic> topics = topicService.getTopicByUserId(userId);
        model.addAttribute("topics", topics);

        return "topic/myTopic";
    }
    @GetMapping("/deleteMyTopic/{topicId}")
    public String deleteMyTopic(@PathVariable("topicId") int topicId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        topicService.deleteTopic(topicId);
        return "redirect:/toMyTopic/" + user.getUserId();
    }

}
